<template>
  <div v-if="visible" class="modal-mask">
    <div class="modal-wrapper">
      <div class="modal-container">
        <button class="modal-close" @click="handleClose" aria-label="关闭弹窗">×</button>
        
        <!-- 确认删除弹窗 -->
        <div v-if="type === 'confirm'">
          <h3>{{ title || '确认删除' }}</h3>
          <p v-html="message"></p>
          <div class="modal-footer">
            <button class="btn delete-btn" @click="handleConfirm">确认删除</button>
            <button class="btn" @click="handleCancel">取消</button>
          </div>
        </div>
        
        <!-- 删除成功弹窗 -->
        <div v-else-if="type === 'success'">
          <h3>{{ title || '删除成功' }}</h3>
          <p v-html="message"></p>
          <p>{{ countdownSeconds }}s后跳转回{{ redirectText }}</p>
          <div class="modal-footer">
            <button class="btn" @click="handleRedirect">确认</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DeleteModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'confirm',
      validator: value => ['confirm', 'success'].includes(value)
    },
    title: {
      type: String,
      default: ''
    },
    message: {
      type: String,
      required: true
    },
    redirectText: {
      type: String,
      default: '主页面'
    },
    countdown: {
      type: Number,
      default: 5
    }
  },
  data() {
    return {
      countdownSeconds: this.countdown,
      countdownTimer: null
    }
  },
  watch: {
    visible(newVal) {
      if (newVal && this.type === 'success') {
        this.startCountdown()
      } else {
        this.stopCountdown()
      }
    },
    type(newVal) {
      if (newVal === 'success' && this.visible) {
        this.startCountdown()
      } else {
        this.stopCountdown()
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    handleCancel() {
      this.$emit('cancel')
    },
    handleConfirm() {
      this.$emit('confirm')
    },
    handleRedirect() {
      this.$emit('redirect')
    },
    startCountdown() {
      this.countdownSeconds = this.countdown
      this.countdownTimer = setInterval(() => {
        this.countdownSeconds--
        if (this.countdownSeconds <= 0) {
          this.handleRedirect()
        }
      }, 1000)
    },
    stopCountdown() {
      if (this.countdownTimer) {
        clearInterval(this.countdownTimer)
        this.countdownTimer = null
      }
    }
  },
  beforeDestroy() {
    this.stopCountdown()
  }
}
</script>

<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
}

.modal-container {
  background: #fff;
  padding: 30px 40px;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.33);
  min-width: 320px;
  text-align: center;
  position: relative;
}

.modal-footer {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  gap: 20px;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: #166d80;
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 1.5em;
  line-height: 32px;
  text-align: center;
  cursor: pointer;
  transition: background 0.2s;
  z-index: 10;
}

.modal-close:hover {
  background: #e74c3c;
  color: #fff;
}

.btn {
  padding: 8px 25px;
  margin: 0 10px;
  border: none;
  border-radius: 4px;
  font-size: 0.9em;
  cursor: pointer;
  transition: all 0.3s ease;
  background: none;
  border: 1px solid #166d80;
  color: #166d80;
  border-radius: 15px;
}

.btn:hover {
  background-color: #166d80;
  color: white;
}

.delete-btn {
  background-color: #e74c3c;
  color: white;
  border-color: #e74c3c;
}

.delete-btn:hover {
  background-color: #b71c1c;
  color: white;
}
</style> 